package com.sugon.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.RandomUtil;
import com.sugon.dao.SparePartKeyDao;
import com.sugon.entity.SparePartEntity;
import com.sugon.entity.SparePartKey;
import com.sugon.entity.SparePartKeyCj;
import com.sugon.modules.sparepart.consts.KeySpEaProgress;
import com.sugon.service.SparePartKeyService;
import com.sugon.utils.Constant;
import com.sugon.utils.ShiroUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 * 关键备件Service实现类
 *
 * @author YuXD
 * @date 2023-06-23
 */
@Service("sparePartKeyService")
public class SparePartKeyServiceImpl implements SparePartKeyService {

    @Autowired
    private SparePartKeyDao sparePartKeyDao;

    @Override
    public SparePartKey checkKeySparePart(SparePartEntity sparePart) {
        // 校验该备件是否已列为关键备件。如果已经是，则只直接返回。否则则新建保存后返回
        List<SparePartKey> existedList = queryList(sparePart.getId());
        if (CollectionUtil.isNotEmpty(existedList)) {
            return existedList.get(0);
        }
        SparePartKey sparePartKey = new SparePartKey();
        sparePartKey.setSpareId(sparePart.getId());
        // 默认直接进行审批
        sparePartKey.setProgress(KeySpEaProgress.ZR_ING);
        sparePartKey.setLife(0L);
        save(sparePartKey);
        return sparePartKey;
    }

    @Override
    public List<SparePartKey> queryList(String spareId) {
        Map<String, Object> param = MapUtil.newHashMap();
        param.put("spareId", spareId);
        List<SparePartKey> sparePartKeyList = sparePartKeyDao.queryList(param);
        return Optional.ofNullable(sparePartKeyList).orElse(CollectionUtil.newArrayList());
    }

    @Override
    public List<SparePartKeyCj> queryCjKeySpList(String keySpId, String spId, String devId, String position) {
        List<SparePartKeyCj> cjKeySpList = sparePartKeyDao.queryCjKeySpList(keySpId, spId, devId, position);
        return Optional.ofNullable(cjKeySpList).orElse(CollectionUtil.newArrayList());
    }

    @Override
    public List<SparePartKey> queryList(Map<String, Object> param) {
        return sparePartKeyDao.queryList(param);
    }

    @Override
    public int queryTotal(Map<String, Object> param) {
        return sparePartKeyDao.queryTotal(param);
    }

    @Override
    public boolean save(SparePartKey sparePartKey) {
        sparePartKey.setId(RandomUtil.randomString(36));
        sparePartKey.setDelFlag(Constant.IS_NOT_DEL_INT);
        sparePartKey.setCreateBy(ShiroUtils.getUserId());
        sparePartKey.setCreateTime(DateUtil.date());
        return sparePartKeyDao.save(sparePartKey) > 0;
    }

    @Override
    public boolean save(SparePartKeyCj cjKeySp) {
        cjKeySp.setId(RandomUtil.randomString(36));
        cjKeySp.setDelFlag(Constant.IS_NOT_DEL_INT);
        cjKeySp.setCreateBy(ShiroUtils.getUserId());
        cjKeySp.setCreateTime(DateUtil.date());
        return sparePartKeyDao.saveCjKeySp(cjKeySp) > 0;
    }
}
